package com.dingding.study.service.事务失效;

import com.dingding.study.entity.User;
import com.dingding.study.mapper.UserMapper;
import com.dingding.study.service.impl.UserServiceImpl;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
public class TransactionTestImpl implements TransactionTest {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    TransactionTestImpl transactionTest;


    @Override
    public void payOrder() {

//        TransactionTest bean = applicationContext.getBean(TransactionTest.class);

        TransactionTestImpl bean = applicationContext.getBean(TransactionTestImpl.class);
//        System.out.println(bean.getClass());
//        userMapper.insertOrder(UUID.randomUUID().toString());

//        bean.pay();
//        TransactionTestImpl transactionTest = (TransactionTestImpl) AopContext.currentProxy();
        System.out.println("service下是否代理类:"+ AopUtils.isAopProxy(bean));
        System.out.println("Autowired获取是否代理类:"+ AopUtils.isAopProxy(transactionTest));
        System.out.println(transactionTest.equals(bean));
//        applicationContext.getBean(Obgect.class)
    }

    @Override
    @Transactional
    public void deductStock() {
        userMapper.updateStock();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW )
    public void insertUser() {
        User user = new User();
        user.setUsername("liugongding");
        userService.save(user);
//        userMapper.insertUser("liugongding");
    }

    @Override
    @Transactional
    public void pay() {
//        payOrder();
        this.deductStock();
        this.insertUser();
        int a = 1/0;

    }

}
